package io.eleKing.boot.weather.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.eleKing.boot.framework.exception.BusinessException;
import io.eleKing.boot.framework.page.OrderByItem;
import io.eleKing.boot.framework.page.OrderMapping;
import io.eleKing.boot.framework.page.Paging;
import io.eleKing.boot.weather.dto.WeatherDayDataDto;
import io.eleKing.boot.weather.entity.WeatherDayData;
import io.eleKing.boot.weather.mapper.WeatherDayDataMapper;
import io.eleKing.boot.weather.query.WeatherDayDataQuery;
import io.eleKing.boot.weather.service.WeatherDayDataService;
import io.eleKing.boot.weather.vo.WeatherDayDataVo;
import io.eleKing.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 *  服务实现类
 *
 * @author eleKing
 * @since 2025-04-15
 */
@Slf4j
@Service
public class WeatherDayDataServiceImpl extends ServiceImpl<WeatherDayDataMapper, WeatherDayData> implements WeatherDayDataService {

    @Autowired
    private WeatherDayDataMapper weatherDayDataMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addWeatherDayData(WeatherDayDataDto dto) {
        WeatherDayData weatherDayData = new WeatherDayData();
        BeanUtils.copyProperties(dto, weatherDayData);
        return save(weatherDayData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateWeatherDayData(WeatherDayDataDto dto) {
        Long id = dto.getId();
        WeatherDayData weatherDayData = getById(id);
        if (weatherDayData == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, weatherDayData);
        return updateById(weatherDayData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteWeatherDayData(Long id) {
        return removeById(id);
    }

    @Override
    public WeatherDayDataVo getWeatherDayDataById(Long id) {
        return weatherDayDataMapper.getWeatherDayDataById(id);
    }

    @Override
    public Paging<WeatherDayDataVo> getWeatherDayDataPage(WeatherDayDataQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<WeatherDayDataVo> list = weatherDayDataMapper.getWeatherDayDataPage(query);
        Paging<WeatherDayDataVo> paging = new Paging<>(list);
        return paging;
    }

}
